package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

import model.ImageProcessor;

public class WindowMain extends JFrame implements ActionListener {

	private ImageProcessor imageProcessor;
	private BufferedImage imagenOriginal;
	private BufferedImage imagenModificada;

	private JPanel contentPane;
	private JLabel lblImagenOriginal;
	private JLabel lblImagenModificada;
	private JRadioButtonMenuItem[] rdbtnmntmPorcentajesDeGrises;
	private JTabbedPane tabbedPane;
	private JMenu mnAplicarFiltro;

	/**
	 * Create the frame.
	 */
	public WindowMain() {
		imageProcessor = new ImageProcessor(null);

		Dimension dimension = new Dimension(800, 600);
		setSize(dimension);
		setMinimumSize(dimension);
		setLocationRelativeTo(null);
		setTitle("Procesador de Imagenes");
		setExtendedState(Frame.MAXIMIZED_BOTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(0, 1, 0, 0));

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnArchivo = new JMenu("Archivo");
		menuBar.add(mnArchivo);

		JMenuItem mntmAbrirImagen = new JMenuItem("Abrir Imagen");
		mntmAbrirImagen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openImage();
			}
		});
		mnArchivo.add(mntmAbrirImagen);

		JMenuItem mntmGuardarImagen = new JMenuItem("Guardar Imagen");
		mnArchivo.add(mntmGuardarImagen);

		JMenuItem mntmSalir = new JMenuItem("Salir");
		mntmSalir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(EXIT_ON_CLOSE);
			}
		});
		mnArchivo.add(mntmSalir);

		JMenu mnOpciones = new JMenu("Opciones");
		menuBar.add(mnOpciones);

		mnAplicarFiltro = new JMenu("Aplicar Filtro");
		mnAplicarFiltro.setEnabled(false);
		mnOpciones.add(mnAplicarFiltro);

		JMenuItem mntmNegativo = new JMenuItem("Negativo");
		mntmNegativo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				applyFilter(ImageProcessor.NEGATIVE, 0);
			}
		});
		mnAplicarFiltro.add(mntmNegativo);

		JMenu mnEscalaDeGrises = new JMenu("Escala de grises");
		mnAplicarFiltro.add(mnEscalaDeGrises);

		ButtonGroup buttonGroup = new ButtonGroup();
		rdbtnmntmPorcentajesDeGrises = new JRadioButtonMenuItem[10];
		for (int i = 0; i < rdbtnmntmPorcentajesDeGrises.length; i++) {
			JRadioButtonMenuItem rdbtnmntmPorciento = new JRadioButtonMenuItem(
					((i + 1) * 10) + "%");
			rdbtnmntmPorcentajesDeGrises[i] = rdbtnmntmPorciento;
			rdbtnmntmPorciento.addActionListener(this);
			buttonGroup.add(rdbtnmntmPorciento);
			mnEscalaDeGrises.add(rdbtnmntmPorciento);
		}

		JMenuItem mntmEscalaDeGrisesYIQ = new JMenuItem("Escala de grises YIQ");
		mntmEscalaDeGrisesYIQ.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				applyFilter(ImageProcessor.GRAY_SCALE_YIQ, 0);
			}
		});
		mnAplicarFiltro.add(mntmEscalaDeGrisesYIQ);

		JMenuItem mntmSepia = new JMenuItem("Sepia");
		mntmSepia.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				applyFilter(ImageProcessor.SEPIA, 0);
			}
		});
		mnAplicarFiltro.add(mntmSepia);

		JMenuItem mntmRojo = new JMenuItem("Rojo");
		mntmRojo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				applyFilter(ImageProcessor.RED, 0);
			}
		});
		mnAplicarFiltro.add(mntmRojo);

		JMenuItem mntmVerde = new JMenuItem("Verde");
		mntmVerde.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				applyFilter(ImageProcessor.GREEN, 0);
			}
		});
		mnAplicarFiltro.add(mntmVerde);

		JMenuItem mntmAzul = new JMenuItem("Azul");
		mntmAzul.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				applyFilter(ImageProcessor.BLUE, 0);
			}
		});
		mnAplicarFiltro.add(mntmAzul);

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane);
		JPanel panelOriginal = new JPanel();
		tabbedPane.addTab("Imagen Original", null, panelOriginal);
		panelOriginal.setLayout(new BorderLayout(0, 0));

		lblImagenOriginal = new JLabel();
		panelOriginal.add(lblImagenOriginal);
		JPanel panelModificada = new JPanel();
		tabbedPane.addTab("Imagen Modificada", null, panelModificada);
		panelModificada.setLayout(new BorderLayout(0, 0));

		lblImagenModificada = new JLabel();
		panelModificada.add(lblImagenModificada);

	}

	public void openImage() {
		JFileChooser selector = new JFileChooser(
				System.getProperty("user.home") + "\\Desktop");
		selector.setFileFilter(new FileNameExtensionFilter("Image", "jpg",
				"jpeg", "gif", "png", "bmp"));
		int r = selector.showOpenDialog(null);
		if (r == JFileChooser.APPROVE_OPTION) {
			try {
				imageProcessor
						.setImage(ImageIO.read(selector.getSelectedFile()));
				lblImagenOriginal.setIcon(new ImageIcon(imageProcessor
						.getImage()));
				mnAplicarFiltro.setEnabled(true);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void applyFilter(int filterType, int average) {
		imagenModificada = null;
		switch (filterType) {
		case ImageProcessor.NEGATIVE:
			imagenModificada = imageProcessor.applyNegativeFilter();
			break;
		case ImageProcessor.GRAY_SCALE_YIQ:
			imagenModificada = imageProcessor.applyGrayScaleFilterYIQ();
			break;
		case ImageProcessor.GRAY_SCALE_AVERAGE:
			imagenModificada = imageProcessor
					.applyGrayScaleFilterAverage(average);
			break;
		case ImageProcessor.SEPIA:
			imagenModificada = imageProcessor.applySepiaFilter();
			break;
		case ImageProcessor.RED:
			imagenModificada = imageProcessor.applyRedFilter();
			break;
		case ImageProcessor.GREEN:
			imagenModificada = imageProcessor.applyGreenFilter();
			break;
		case ImageProcessor.BLUE:
			imagenModificada = imageProcessor.applyBlueFilter();
			break;
		}
		lblImagenModificada.setIcon(new ImageIcon(imagenModificada));
		tabbedPane.setSelectedIndex(1);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		for (int i = 0; i < rdbtnmntmPorcentajesDeGrises.length; i++) {
			if (e.getSource() == rdbtnmntmPorcentajesDeGrises[i]) {
				applyFilter(ImageProcessor.GRAY_SCALE_AVERAGE, i + 1);
			}
		}
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager
							.getSystemLookAndFeelClassName());
					WindowMain frame = new WindowMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
